﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AutoEditUI
{

    public static class PicConverter
    {
        public static byte[] BitmapSource2ByteArray(BitmapSource bitmap)
        {
            using (var stream = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(stream);
                return stream.ToArray();
            }
        }

        public static BitmapSource ByteArray2BitmapSource(byte[] buffer)
        {
            using (var stream = new MemoryStream(buffer))
            {
                return BitmapFrame.Create(stream,
                    BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            }
        }
    }
    public class ValueSync
    {
        public static double mmTypes= 3.7808;

        public static UIModel UIModelGet(MainWindow mainWindow, Canvas workCanvas, UIModel gUIModel)
        {

            gUIModel.W = workCanvas.Width/ mmTypes;
            gUIModel.H= workCanvas.Height / mmTypes;

            foreach (FrameworkElement item in workCanvas.Children)
            {
                if (gUIModel.ItemIcons != null)
                    foreach (ItemIcon itemIcon in gUIModel.ItemIcons)
                    {
                        if (itemIcon.Tag == (string)item.Tag)
                        {
                            itemIcon.ItemName = item.Name;
                          //  itemIcon.ItemSource = (string)((Image)item).Source.ToString();
                            byte[] bytesBitmapEmbedded = PicConverter.BitmapSource2ByteArray((BitmapSource)((Image)item).Source);
                            string result = System.Convert.ToBase64String(bytesBitmapEmbedded);
                            itemIcon.ItemBin = result;
                            itemIcon.W = item.ActualWidth;
                            itemIcon.H = item.ActualHeight;
                            itemIcon.X = (double)((Image)item).GetValue(Canvas.LeftProperty);
                            itemIcon.Y = (double)((Image)item).GetValue(Canvas.TopProperty);
                            break;
                        }
                    }
                if (gUIModel.ItemBarcodes != null)
                    foreach (ItemBarcode itemBarcode in gUIModel.ItemBarcodes)
                    {
                        if (itemBarcode.Tag == (string)item.Tag)
                        {
                            itemBarcode.ItemName = item.Name;
                            itemBarcode.W = item.ActualWidth;
                            itemBarcode.H = item.ActualHeight;
                            itemBarcode.X = (double)((Image)item).GetValue(Canvas.LeftProperty);
                            itemBarcode.Y = (double)((Image)item).GetValue(Canvas.TopProperty);
                            break;
                        }
                    }

                if (gUIModel.ItemQRs != null)
                    foreach (ItemQR itemQR in gUIModel.ItemQRs)
                    {
                        if (itemQR.Tag == (string)item.Tag)
                        {
                            itemQR.ItemName = item.Name;
                            itemQR.W = item.ActualWidth;
                            itemQR.H = item.ActualHeight;
                            itemQR.X = (double)((Image)item).GetValue(Canvas.LeftProperty);
                            itemQR.Y = (double)((Image)item).GetValue(Canvas.TopProperty);
                            break;
                        }
                    }

                if (gUIModel.ItemRects != null)
                    foreach (ItemRect itemRect in gUIModel.ItemRects)
                    {
                        if (itemRect.Tag == (string)item.Tag)
                        {
                            itemRect.ItemName = item.Name;
                            itemRect.W = item.ActualWidth;
                            itemRect.H = item.ActualHeight;
                            itemRect.X = (double)((Rectangle)item).GetValue(Canvas.LeftProperty);
                            itemRect.Y = (double)((Rectangle)item).GetValue(Canvas.TopProperty);
                            break;
                        }
                    }

                if (gUIModel.ItemTexts != null)
                    foreach (ItemText itemText in gUIModel.ItemTexts)
                    {
                        if (itemText.Tag == (string)item.Tag)
                        {
                            //if (string.IsNullOrEmpty(itemText.Text_)) {
                            //    itemText.Text = ((TextBlock)item).Text;
                            //}
                            //else {
                            //    itemText.Text = ((TextBlock)item).Text.Replace(itemText.Text_,"");
                            //}
                            itemText.ItemName = item.Name;
                            itemText.W = item.ActualWidth;
                            itemText.H = item.ActualHeight;
                            itemText.FontFamily = ((TextBlock)item).FontFamily.ToString();
                            itemText.FontSize = ((TextBlock)item).FontSize;
                            itemText.FontWeight = ((TextBlock)item).FontWeight.ToOpenTypeWeight();
                            itemText.X = (double)((TextBlock)item).GetValue(Canvas.LeftProperty);
                            itemText.Y = (double)((TextBlock)item).GetValue(Canvas.TopProperty);
                            break;
                        }
                    }
            }
            return gUIModel;
        }

        internal static void UIModelSet(MainWindow mainWindow, UIModel gUIModel, System.Windows.Controls.Canvas workCanvas)
        {

            MainWindow.gItems.Clear();
            MainWindow.xyRelate.Clear();
            MainWindow.xyRelate.Clear();

            

            workCanvas.Width= gUIModel.W* mmTypes;
            workCanvas.Height= gUIModel.H* mmTypes;

            mainWindow.TextBoxWidth.Text = gUIModel.W.ToString();
            mainWindow.TextBoxHeight.Text = gUIModel.H.ToString();
            if (gUIModel.ItemIcons != null)
                foreach (ItemIcon item in gUIModel.ItemIcons)
                {
                    UIInit.AddIcon(mainWindow, workCanvas, item);
                }
            if (gUIModel.ItemBarcodes != null)
                foreach (ItemBarcode item in gUIModel.ItemBarcodes)
                {
                    UIInit.AddBarcode(mainWindow, workCanvas, item);
                }
            //foreach (ItemCircle item in gUIModel.ItemCircles)
            //{
            //    UIInit.AddCircle(mainWindow, workCanvas, item);
            //}
            if (gUIModel.ItemQRs != null)
                foreach (ItemQR item in gUIModel.ItemQRs)
                {
                    UIInit.AddQR(mainWindow, workCanvas, item);
                }
            if (gUIModel.ItemRects != null)
                foreach (ItemRect item in gUIModel.ItemRects)
                {
                    UIInit.AddRect(mainWindow, workCanvas, item);
                }
            if (gUIModel.ItemTexts != null)
                foreach (ItemText item in gUIModel.ItemTexts)
                {
                    UIInit.AddText(mainWindow, workCanvas, item);
                } 
            if (gUIModel.ItemCanvass != null)
                foreach (ItemCanvas item in gUIModel.ItemCanvass)
                {
                    UIInit.AddCanvas(mainWindow, workCanvas, item);
                }
            foreach (UIItem item in MainWindow.gItems)
            {
                if (!MainWindow.xyRelate.Contains(item.Tag)) {
                    MainWindow.xyRelate.Add(item.Tag);
                }
                //if (!MainWindow.yRelate.Contains(item.Tag))
                //{
                //    MainWindow.yRelateNot.Add(item.Tag);
                //}
            }
        }
    }
}